package com.example.app3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    EditText name,number;
    Button add,delete,update,select;
    ListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
//      list的点击事件
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView simple_name = view.findViewById(R.id.simple_name);
                TextView simple_number = view.findViewById(R.id.simple_number);
                name.setText(simple_name.getText().toString());
                number.setText(simple_number.getText().toString());
            }
        });
//      查询
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<Map<String, String>> maps = queryAllContactors();
                SimpleAdapter simpleAdapter = new SimpleAdapter(getApplicationContext(), maps, R.layout.single_info, new String[]{"name", "number"}, new int[]{R.id.simple_name, R.id.simple_number});
                list.setAdapter(simpleAdapter);
            }
        });
//      删除
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = name.getText().toString();
                ContentResolver resolver = getContentResolver();
                resolver.delete(ContactsContract.RawContacts.CONTENT_URI,
                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+"=?",new String[]{s});
                Toast.makeText(getApplicationContext(), "联系人删除成功", Toast.LENGTH_SHORT).show();
            }
        });
//      增加
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = name.getText().toString();
                String s1 = number.getText().toString();
                insert(s,s1);
                Toast.makeText(getApplicationContext(), "联系人添加成功", Toast.LENGTH_SHORT).show();
            }
        });
//     修改联系人
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = name.getText().toString();
                String s1 = number.getText().toString();
                ContentResolver resolver = getContentResolver();
                resolver.delete(ContactsContract.RawContacts.CONTENT_URI,
                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+"=?",new String[]{s});
                insert(s,s1);
                Toast.makeText(getApplicationContext(), "联系人更新成功", Toast.LENGTH_SHORT).show();
            }
        });
    }
    public void init(){
        name = findViewById(R.id.name);
        number = findViewById(R.id.number);
        add = findViewById(R.id.add);
        delete = findViewById(R.id.delete);
        update = findViewById(R.id.update);
        select = findViewById(R.id.select);
        list = findViewById(R.id.list);
        List<Map<String, String>> maps = queryAllContactors();
        SimpleAdapter simpleAdapter = new SimpleAdapter(getApplicationContext(), maps, R.layout.single_info, new String[]{"name", "number"}, new int[]{R.id.simple_name, R.id.simple_number});
        list.setAdapter(simpleAdapter);
    }
    //查询到所有的联系人
    public List<Map<String,String>> queryAllContactors(){
        ContentResolver resolver = getContentResolver();
        String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
        Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
                null, null, null);
        List<Map<String,String>> listItems=new ArrayList<>();
        while (cursor.moveToNext()){
            String name = cursor.getString(0);
            String number = cursor.getString(1);
            System.out.println("访问到cursor取得的名字:        "+name);
            System.out.println("访问到cursor取得的号码:        "+number);
            Map<String, String> map = new HashMap<>();
            map.put("name", name);
            map.put("number", number);
            listItems.add(map);
        }
        cursor.close();
        return listItems;
    }
    //  插入联系人
    public void insert(String name, String phoneNumber){
        ContentValues values = new ContentValues();
        Uri rawContactUri = getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values);
        long rawContactId = ContentUris.parseId(rawContactUri);
        values.clear();

        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
        values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, name);
        getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
        values.clear();

        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
        values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber);
        values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
        getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
        values.clear();
    }

}